package _5_multithread._3_pool;

import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class 自带的几种线程池 {

  public static void main(String[] args) throws InterruptedException {
    Thread.sleep(5000);
    ExecutorService service1 = Executors.newCachedThreadPool(); // 1min 才销毁
    // Intel I5 4CPU, 内存8G  : hotspot JDK8 报错  OutOfMemoryError: unable to create new native thread
    // hotspot JDK8 报错  OutOfMemoryError: unable to create new native thread
    for (int i = 0; i < 1000000; i++) {
      service1.execute(new MyTask());
    }
    System.out.println("提交完成");

    ExecutorService service2 = Executors.newSingleThreadExecutor(); // 1min 才销毁
  }
}

class MyTask implements Runnable {
  private Set<String> set;
  public MyTask() {
    set = new HashSet<>();
  }

  @Override
  public void run() {
    System.out.println(Runtime.getRuntime().availableProcessors());
    try {
      Thread.sleep(10000000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
}